<template>
  <div class="container">
    <div class="content">
      <div class="title">
        为你推荐
      </div>

      <div @click="goDetail(pic)" class="item" v-for="(pic,index) in pics" :key="index">
        <div style="height:224px;overflow:hidden;"><img v-lazy="pic.goods_url" alt="图片加载失败"></div>
        <div class="text">
          <p style="color:gray;margin:10px 3px 10px 3px;">{{pic.goods_title}}</p>
        </div>
        <div class="operation">
          <span style="color:#e5505a;">￥{{pic.goods_price}}</span>
          <button @click.stop="addCar(pic.goods_id)">购物车</button>
          <button @click.stop="purchase(pic)" style="background:#e5505a;">购买</button>
        </div>
      </div>

      <div style="clear:both;"></div>
    </div>
  </div>
</template>

<script>
import {reqGoodsRecommend,reqAddCar} from '../../api'
export default {
    name:'Recommend',
    data() {
      return {
        pics:[]
      }
    },
    async mounted() {
      this.pics = await reqGoodsRecommend();
    },
    methods: {
      goDetail(item){ 
        const array = JSON.stringify(item)
        this.$router.push({path:'/detail',query:{value:array}})
      },
      addCar(id){ //添加到购物车
        if (!this.$local.isExist('key')) {
            this.$message.error('登录令牌不存在！')
            this.$router.push('/user/info')
        }else{
            if(reqAddCar(id,this.$local.get('key')).exist === false) {
                this.$message.error('登录令牌失效，请重新登录！')
                this.$router.push('/user/info')
            }else{
                this.$message.success('添加到购物车成功,可前往购物车查看！')
            }
        }
      },
      purchase(item){ //购买
        if (this.$local.isExist('key')) {
          item.car_goods_num='1'
          let arr = []
          arr.push(item)
          const array = JSON.stringify(arr)
          this.$router.push({path:'/settlement',query:{value:array}})
        }else{
          this.$message.error('登录令牌不存在！')
          this.$router.push('/user/info')
        }
      }
    }
}
</script>

<style scoped>
  .item{
    width: 236px;
    float: left;
    margin-right: 20px;
    border-radius: 8px;
    overflow: hidden;
    background: #fff;
    transform: translateY(0);
    /* 移回也需要0.4s时间过渡 */
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .item:nth-of-type(4n+5){
    margin-right: 0;
  }
  .item:nth-of-type(n+6){
    margin-top: 20px;
  }
  .item:hover{
    box-shadow: 0 0 15px 0 gray;
    transform: translateY(-5px);
    /* 0.4s完成transform移动效果*/
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .operation{
    margin: 3px 0 5px 5px;
  }
  .operation button{
    float: right;
    margin-right: 5px;
    margin-bottom: 5px;
  }
  .text{
    /* color: rgba(255, 255, 255, 0.75); */
    font-size: 14px;
    max-width: 100%;
    height: 40px;
    padding:0 5px;
  }
  .text p{
    font-size: 12px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
  .item img{
    max-width: 100%;
    /* max-height: 224px; */
    /* border-radius: 8px; */
  }
</style>